package com.paomi.client.fragment.order;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.R;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.fragment.order.Adapter.OrderListAdapter;
import com.paomi.client.fragment.order.model.OrderListBean;
import com.paomi.client.fragment.order.presenter.OrderCompletPre;
import com.paomi.client.fragment.order.presenter.OrderPre;
import com.paomi.client.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * 已完成订单
 */
public class CompleteOrderFragment extends BaseLCEFragment<OrderListBean.OrderData, OrderCompletPre> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int pageNum=1;
    OrderListAdapter adapter;
    List<OrderListBean.OrderGoods> list = new ArrayList<>();//子项列表数据
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        setPtrFrameLayout(ptrRefresh);
        initAdapter();
        getPresenter().getListOrder("4",pageNum);
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return recyclerView;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_order_list;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_k_dingdan,0,0);
        empty.setText(getResources().getString(R.string.order_null));
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderListBean.OrderData item) {
        RecyclerView recyclerView = helper.itemView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
        adapter = new OrderListAdapter(getActivity(), item);
        recyclerView.setAdapter(adapter);
        adapter.setData(item.getOrderGoodsTickets());
        if (item.getState() == 0) {// 0 未支付
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_pay));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.colorFF3B30));
            helper.setVisible(R.id.tv_pay_now, true);
        } else if (item.getState() == 1) {// 1 已支付
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_delivery));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.text_black_32));
        } else if (item.getState() == 2) {//2 待发货
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_delivery));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.colorFF3B30));
        } else if (item.getState() == 3) {//3 待收货
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_state_ring));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.colorFF3B30));
            helper.setVisible(R.id.tv_received_success, true);
        } else if (item.getState() == 5) {//5 已完成（已评价）
            helper.setText(R.id.tv_states, getResources().getString(R.string.completed));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.text_black_32));
            helper.setVisible(R.id.tv_buy_again, true);
            //显示删除按钮
            helper.setVisible(R.id.delete_iv, true);
        } else if (item.getState() == 7) {// 7 已退款
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_pay));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.text_black_32));
        } else if (item.getState() == 8){//是不是缺一个取消8交易关闭
            helper.setText(R.id.tv_states, getResources().getString(R.string.order_state_can));
            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.colorFF3B30));
            //显示删除按钮
            helper.setVisible(R.id.delete_iv, true);
        }
//         else if (item.getState() == 4) {//4 待评价（已收货）
//            helper.setText(R.id.tv_states, getResources().getString(R.string.order_pay));
//            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.text_black_32));
//            helper.setVisible(R.id.tv_pay_now,true);
//        }
//        else if (item.getState() == 6) {// 6 退款申请中
//            helper.setText(R.id.tv_states, getResources().getString(R.string.order_pay));
//            helper.setTextColor(R.id.tv_states, getResources().getColor(R.color.text_black_32));
//            helper.setVisible(R.id.tv_pay_now,true);
//        }
        //显示有几件商品
        helper.setText(R.id.pay_intro, getResources().getString(R.string.order_intro,item.getOrderGoodsTickets().size()));
        //显示总价格
        if(item.getPayAmount()>=0){
            helper.setText(R.id.pay_money,"¥"+""+item.getPayAmount());
        }else {
            helper.setText(R.id.pay_money, "¥" + "" + item.getTotalTicketAmount());
        }
        //删除订单操作
        helper.itemView.findViewById(R.id.delete_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().deleteBtn(getActivity(),"" + item.getOrderId());
            }
        });
        //取消订单的操作
        TextView order_btn_cancel = helper.itemView.findViewById(R.id.tv_clear_order);
        order_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().getOrderCancel("" + item.getOrderId());
            }
        });
        //确认收货的操作
        TextView order_btn_sure = helper.itemView.findViewById(R.id.tv_received_success);
        order_btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().getOrderConfim("" + item.getOrderId());
            }
        });
        //立即支付的操作
        TextView tv_pay_now = helper.itemView.findViewById(R.id.tv_pay_now);
        tv_pay_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    @Override
    protected void onItemClick(View view, OrderListBean.OrderData item) {

    }

    @Override
    public OrderCompletPre createPresenter() {
        return new OrderCompletPre();
    }
    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
    @Subscribe
    public void onEvent(BaseJSON result) {
        if (result.returnMessage.equals("订单取消")) {
            ToastUtil.getToastEmail().ToastShow(getActivity(), null, 1, R.layout.toast_view, getResources().getString(R.string.order_state_can));
            getPresenter().getListOrder("5",pageNum);
        } else if (result.returnMessage.equals("订单删除")) {
            ToastUtil.getToastEmail().ToastShow(getActivity(), null, 1, R.layout.toast_view, getResources().getString(R.string.delete_success));
            getPresenter().getListOrder("5",pageNum);
        }else if (result.returnMessage.equals("确认收货")) {
            ToastUtil.getToastEmail().ToastShow(getActivity(), null, 1, R.layout.toast_view, getResources().getString(R.string.delete_success));
            pageNum=1;
            getPresenter().getListOrder("5",pageNum);
        }
    }

    @Override
    public void setData(List<OrderListBean.OrderData> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        if(getmAdapterWrapper()!=null){
            if(data.size()<=9){//移除FooterView
                getmAdapterWrapper().setLoadItemVisibility(false);
            }
            getmLoadMoreHelper().setLoadMoreFinish();
            getmAdapterWrapper().notifyDataSetChanged();
        }else{
            getAdapter().notifyDataSetChanged();
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1003){
            if(resultCode==2003){
                pageNum=1;
                getPresenter().getListOrder("5",pageNum);
            }
        }
    }
}
